<?php
namespace app\wechat\controller;
use app\api\service\Comment;
use app\api\service\Order;
use think\Db;

class WAddress extends WCommon
{
    // 收货地址列表
    public function index() {
        $member_id = session('member_id');
        $data = db('address')->alias('a')->field('a.*, p.region_name r_province, c.region_name r_city, e.region_name r_area')
                ->join('mob_region p', 'p.region_id = a.province')
                ->join('mob_region c', 'c.region_id = a.city')
                ->join('mob_region e', 'e.region_id = a.area')
                ->where('member_id', $member_id)->order('id desc')->select();
        $this->assign('data', $data);
        return view();
    }

    // 编辑/添加 收货地址
    public function save_address() {
        $id = input('param.id');
        $member_id = session('member_id');
        if (request()->isAjax()) {
            if (!input('?param.address_ids') && !input('param.address_ids')) {
                return json(['code' => 0, 'msg' => '地址不能为空！']);
            }
            $address_ids = explode(',', input('param.address_ids'));
            $data = [
                'linkman' => input('param.linkman'),
                'phone' => input('param.phone'),
                'province' => $address_ids[0],
                'city' => $address_ids[1],
                'area' => $address_ids[2],
                'street' => input('param.street'),
                'address' => input('param.address'),
                'postcode' => input('param.postcode'),
            ];
            if ($id > 0) {
                $rs = db('address')->where('id', $id)->where('member_id', $member_id)->update($data);
            }else {
                $count = db('address')->where('member_id', $member_id)->count();
                if ($count >= 10) {
                    return json(['code' => 0, 'msg' => '最多添加10个收获地址！']);
                }
                $data['member_id'] = $member_id;
                $rs = db('address')->insert($data);
            }
            if ($rs) {
                return json(['code' => 200, 'msg' => '保存成功！！！']);
            } else {
                return json(['code' => 0, 'msg' => '保存失败！！！']);
            }
        } else {
            $data = [];
            if ($id > 0) {
                $data = db('address')->alias('a')->field('a.*, p.region_name r_province, c.region_name r_city, e.region_name r_area')
                    ->join('mob_region p', 'p.region_id = a.province')
                    ->join('mob_region c', 'c.region_id = a.city')
                    ->join('mob_region e', 'e.region_id = a.area')
                    ->where('id', $id)->where('member_id', $member_id)->find();
            }
            $this->assign('data', $data);
            return view();
        }
    }

    // 删除收货地址
    public function delete_address() {
        $id = input('param.id');
        $member_id = session('member_id');
        $address = db('address')->where('id', $id)->find();
        if ($address['is_default'] == 1) {
            return json(['code' => 0, 'msg' => '默认地址不能删除！！']);
        }

        $rs = db('address')->where('id', $id)->where('member_id', $member_id)->delete();
        if ($rs !== false) {
            return json(['code' => 200, 'msg' => 'success']);
        } else {
            return json(['code' => 0, 'msg' => '删除失败！']);
        }
    }

    // 设置默认收获地址
    public function save_default_address() {
        $id = input('param.id');
        $member_id = session('member_id');
        // 启动事务
        Db::startTrans();
        try {
            Db::table('mob_address')->where('member_id', $member_id)->update(['is_default' => 0]);

            Db::table('mob_address')->where('id', $id)->where('member_id', $member_id)->update(['is_default' => 1]);
            Db::commit();
            return json(['code' => 200, 'msg' => 'success']);
        }catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 0, 'msg' => '操作失败！']);
        }
    }
}
